/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inter;

import Logic.Conectividad;
import Logic.MessageBox;
import Logic.This;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ronaldo
 */
public class CtrMPrima extends javax.swing.JInternalFrame {

    public int Status = 0;
    public int Status2 = 0;

    /**
     * Creates new form CtrMPrima
     */
    public CtrMPrima() {
        initComponents();
        Clean();
    }

    private void Clean() {
        txtCodigo.setText(This.AlfaNum(10));
        cbxProveedor.setSelectedIndex(-1);
        txtNombre.setText("");
        txtExistencia.setText("0");
        txtCosto.setText("0");
        txtCantidad.setText("0");
        cbxProducto.setSelectedIndex(-1);
        Load();
    }

    private void Load() {
        try {
            javax.swing.table.DefaultTableModel Modelo = (javax.swing.table.DefaultTableModel) jTable1.getModel();
            Modelo.setRowCount(0);
            String Sql = "SELECT * FROM MPRIMA";
            java.sql.Statement Cons = Conectividad.Conector.createStatement();
            java.sql.ResultSet Resul = Cons.executeQuery(Sql);
            while (Resul.next()) {
                java.util.Vector v = new java.util.Vector();
                v.add(Resul.getString(1));
                v.add(Resul.getString(2));
                v.add(Resul.getString(3));
                v.add(Resul.getFloat(4));
                v.add(Resul.getFloat(5));
                v.add(Resul.getFloat(6));
                v.add(Resul.getString(7));
                Modelo.addRow(v);
            }
            jTable1.setModel(Modelo);
        } catch (SQLException ex) {
            Logger.getLogger(CtrMPrima.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    //Control de Codigos de Proveedor
    ArrayList CodigoProveedor = new ArrayList();

    public void LoadProveedor() {
        try {
            javax.swing.DefaultComboBoxModel Modelo = (javax.swing.DefaultComboBoxModel) cbxProveedor.getModel();
            Modelo.removeAllElements();
            String Sql = "SELECT COD_PROV,NOMBRE FROM PROVEEDOR";
            java.sql.Statement Cons = Conectividad.Conector.createStatement();
            java.sql.ResultSet Result = Cons.executeQuery(Sql);
            CodigoProveedor.clear();
            int i = 0;
            while (Result.next()) {
                Modelo.addElement(Result.getString(2));
                CodigoProveedor.add(Result.getString(1));
            }

            if (Modelo.getSize() <= 0) {
                setVisible(false);
                Status = 1;
                MessageBox.show(this, "Sin proveedor registrado", "Lo sentimos, pero no se ha registrado proveedor alguno\n"
                        + "Favor de Registrar Proveedor", MessageBox.Error);
            } else {
                Status = 0;
            }
            //txtDescProveedor.setText(CodigoProveedor.get(cbxProveedor.getSelectedIndex()).toString());
        } catch (SQLException ex) {
            Logger.getLogger(CtrMPrima.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    ArrayList CodigoProducto = new ArrayList();

    public void LoadProducto() {
        try {
            javax.swing.DefaultComboBoxModel Modelo = (javax.swing.DefaultComboBoxModel) cbxProducto.getModel();
            Modelo.removeAllElements();
            String Sql = "SELECT COD_PDT,DESCRIP FROM PRODUCTO";
            java.sql.Statement Cons = Conectividad.Conector.createStatement();
            java.sql.ResultSet Result = Cons.executeQuery(Sql);
            while (Result.next()) {
                Modelo.addElement(Result.getString(2));
                CodigoProducto.add(Result.getString(1));
            }
            if (Modelo.getSize() <= 0) {
                MessageBox.show(this, "Sin Producto Registrado", "Lo sentimos, pero no se ha registrado producto alguno\n"
                        + "Favor de registrar producto", MessageBox.Error);
                Status2 = 1;
                setVisible(false);
            } else {
                Status2 = 0;
            }
        } catch (SQLException e) {
            MessageBox.show(this, "Se ha generado un error en Sql", "Error\n" + e.getMessage(), MessageBox.Error);
        }
    }

    private void Save() {
        try {
            String CMPrima,  Nombre, Existencia, Costo, CantProducto, CProducto;
            CMPrima = txtCodigo.getText();

            Nombre = txtNombre.getText();
            Existencia = txtExistencia.getText();
            Costo = txtCosto.getText();
            CantProducto = txtCantidad.getText();
            CProducto = cbxProducto.getSelectedItem().toString();

            String Sql;
            Sql = "INSERT INTO MPRIMA VALUES('" + CMPrima + "','" + CodigoProveedor.get(cbxProveedor.getSelectedIndex()).toString().trim() + "','" + Nombre + "'," + Existencia
                    + "," + Costo + "," + CantProducto + ",'" + CodigoProducto.get(cbxProducto.getSelectedIndex()).toString().trim() + "')";
            java.sql.Statement Cons = Conectividad.Conector.createStatement();
            Cons.executeUpdate(Sql);
            MessageBox.show(this, "Registrado Exitosamente", "Se ha registrado Exitosamente!!!", MessageBox.Informacion);
            Clean();
        } catch (SQLException ex) {
            //Logger.getLogger(CtrMPrima.class.getName()).log(Level.SEVERE, null, ex);
            MessageBox.show(this, "Se ha presentado un Error", "Se ha Presentado un Error\n" + ex.getMessage(), MessageBox.Error);
        } catch (Exception e) {
            MessageBox.show(this, "Se ha presentado un Error", "Se ha Presentado un Error\n" + e.getMessage(), MessageBox.Error);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        cbxProveedor = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        txtExistencia = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtCosto = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtDescProveedor = new javax.swing.JLabel();
        lblSave = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        cbxProducto = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        txtCantidad = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Control de Materia prima");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Datos", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Century", 0, 12))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Century", 0, 12)); // NOI18N
        jLabel1.setText("Codigo Producto");

        txtCodigo.setEditable(false);
        txtCodigo.setFont(new java.awt.Font("Century Schoolbook L", 0, 10)); // NOI18N
        txtCodigo.setPreferredSize(null);

        jLabel2.setFont(new java.awt.Font("Century", 0, 12)); // NOI18N
        jLabel2.setText("Proveedor");

        cbxProveedor.setFont(new java.awt.Font("Century", 0, 12)); // NOI18N
        cbxProveedor.setMinimumSize(new java.awt.Dimension(84, 22));
        cbxProveedor.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbxProveedorItemStateChanged(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Century", 0, 12)); // NOI18N
        jLabel3.setText("Nombre");

        txtNombre.setFont(new java.awt.Font("Century Schoolbook L", 0, 10)); // NOI18N
        txtNombre.setPreferredSize(null);

        txtExistencia.setFont(new java.awt.Font("Century Schoolbook L", 0, 10)); // NOI18N
        txtExistencia.setText("0");
        txtExistencia.setPreferredSize(null);

        jLabel4.setFont(new java.awt.Font("Century", 0, 12)); // NOI18N
        jLabel4.setText("Existencia");

        txtCosto.setFont(new java.awt.Font("Century Schoolbook L", 0, 10)); // NOI18N
        txtCosto.setText("0");

        jLabel5.setFont(new java.awt.Font("Century", 0, 12)); // NOI18N
        jLabel5.setText("Costo");

        txtDescProveedor.setFont(new java.awt.Font("Century", 0, 12)); // NOI18N

        lblSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/sfile.png"))); // NOI18N
        lblSave.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        lblSave.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblSaveMouseClick1(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblSaveMouseExit1(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblSaveMouseEnter1(evt);
            }
        });

        jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/edit-clear.png"))); // NOI18N
        jLabel8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel8MouseClicked(evt);
            }
        });

        jLabel7.setText("Producto");

        cbxProducto.setPreferredSize(new java.awt.Dimension(72, 22));

        jLabel9.setText("Cantidad");

        txtCantidad.setPreferredSize(new java.awt.Dimension(72, 22));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtExistencia, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCosto))
                    .addComponent(txtNombre, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
                        .addComponent(txtDescProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(cbxProveedor, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jLabel9))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtCantidad, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
                    .addComponent(cbxProducto, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblSave)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel1)
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtDescProveedor))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cbxProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(cbxProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtExistencia, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(txtCosto, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblSave)
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Detalle de Datos", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Century", 0, 12))); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Proveedor", "Nombre", "Existencia", "Costo", "Cant. Formula", "Producto"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 808, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void lblSaveMouseClick1(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblSaveMouseClick1
        // TODO add your handling code here:
        Save();
    }//GEN-LAST:event_lblSaveMouseClick1

    private void lblSaveMouseEnter1(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblSaveMouseEnter1
        // TODO add your handling code here:
        lblSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/sfile2.png")));
    }//GEN-LAST:event_lblSaveMouseEnter1

    private void lblSaveMouseExit1(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblSaveMouseExit1
        // TODO add your handling code here:
        lblSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/sfile.png")));
    }//GEN-LAST:event_lblSaveMouseExit1

    private void jLabel8MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel8MouseClicked
        // TODO add your handling code here:
        Clean();
    }//GEN-LAST:event_jLabel8MouseClicked

    private void cbxProveedorItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbxProveedorItemStateChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_cbxProveedorItemStateChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbxProducto;
    private javax.swing.JComboBox cbxProveedor;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel lblSave;
    private javax.swing.JTextField txtCantidad;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtCosto;
    private javax.swing.JLabel txtDescProveedor;
    private javax.swing.JTextField txtExistencia;
    private javax.swing.JTextField txtNombre;
    // End of variables declaration//GEN-END:variables
}
